/* model */
import { 
  ConnectorBizTypeEnum,
  ConnectorConditionTypeEnum,
  ConnectorDialogDetailData,
  ConnectorDialogDetailPaasData, 
  ConnectorEditOptions, 
  ConnectorFieldOperateEnum, 
  ConnectorModule, 
  ConnectorOptionsFieldOption,
  ConnectorToField, 
  ConnectorOptionsFieldOptionValueTypeEnum, 
  CreateConnectorDialogFieldNameEnum, 
  ConnectorOptionsActionEnum,
  ConnectorFieldTypeEnum,
  ConnectorField,
  ConnectorAppModuleListItem,
  ConnectorOptionsAction,
} from '@src/modules/connector/model'
import Field from '@src/model/Field'
/* types */
import { ConnectorGetModuleListResult, ConnectorSaveOptionsParams, ConnectorRuleFormItemType, ConnectorGetAppModuleListItemResult, ConnectorFormCascadeDataSourceType, ConnectorSaveOptionsParamsActionListItem } from '@src/modules/connector/types'
import { 
  connectorToField2FormField,
  isConnectorRuleFormConditionAppFromField,
  isConnectorRuleFormConditionEmpty, 
  isConnectorRuleFormConditionFixedValue, 
  isConnectorRuleFormConditionFromField 
} from '@src/modules/connector/util/form'
/* util */
import { isArray, isEmpty, isNull, isObject, isString, isNumber } from '@src/util/type'
import { createConnectorRuleFormItem } from '@src/modules/connector/util'
import { convertFormValueToServerValue } from '@src/modules/connector/util/convert'
import { uuid } from 'pub-bbx-utils'
import { initialize } from '@src/component/form/util'
import { stringify } from '@src/util/lang/object'
/* util advancedSearch */
import { genComponent } from '@src/component/AdvancedSearch/advancedSearch'
/* types */
// import { ConnectorRuleFormItemType } from '@src/modules/connector/types'

/** 
 * @description 连接器模块列表数据转表单多级菜单数据
 * -- 此方面里面的代码 所有都是由 Github Copilot 生成的
*/
function connectorModuleToFormCascaderDataSource(moduleList: ConnectorModule[]): ConnectorFormCascadeDataSourceType[] {
  
  const dataSource: ConnectorFormCascadeDataSourceType[] = []
  
  moduleList.forEach((module) => {
    const moduleItem: ConnectorFormCascadeDataSourceType = {
      value: module.name,
      label: module.name,
      canSelect: module.canSelect || true,
      children: []
    }
    
    module.children.forEach((form) => {
      
      const formItem: ConnectorFormCascadeDataSourceType = {
        value: form.name,
        label: form.name,
        canSelect: form.canSelect,
      }
      
      if (moduleItem.children) {
        moduleItem.children.push(formItem)
      }
      
    })
    
    dataSource.push(moduleItem)
    
  })
  
  return dataSource
  
}

function connectorAppModuleToFormCascaderDataSource(appModuleList: ConnectorAppModuleListItem[], isDeepItem = false): ConnectorFormCascadeDataSourceType[] {
  const dataSource: ConnectorFormCascadeDataSourceType[] = []
  
  appModuleList.forEach((module: ConnectorAppModuleListItem) => {
    const moduleItem: ConnectorFormCascadeDataSourceType = {
      appId: module.appId,
      appType: isDeepItem ? module.appType : null,
      value: (isDeepItem ? module.uniqueId : module.appId ? module.appId : module.name) as unknown as string,
      label: module.name,
      canSelect: module?.canSelect || (module?.children && module?.children?.length === 0) || false,
      children: [],
      bizType: module.bizType,
      bizTypeId: module.bizTypeId,
      icon: module.logo,
      leaf: module.children && module.children.length === 0
    }
    
    if(module.children && module.children.length > 0) {

      moduleItem.children = connectorAppModuleToFormCascaderDataSource(module.children, isDeepItem)
    }

    dataSource.push(moduleItem)
    
  })
  
  return dataSource
  
}

/** 
 * @description 根据 paas 表单 id 获取 对应模块的 paas 表单数据
*/
function getToFormFromModuleListById(moduleList: ConnectorModule[], id: string): ConnectorDialogDetailPaasData {
  
  let toFormData: ConnectorDialogDetailPaasData = {
    id: '',
    name: '',
    icon: '',
    type: ''
  }
  
  moduleList.forEach((module) => {
    
    module.children.forEach((form) => {
      
      const isToFormDataEmpty = isEmpty(toFormData.id)
      
      if (form.uniqueId === id && isToFormDataEmpty) {
        toFormData = {
          id: form.bizTypeId,
          name: form.name,
          icon: form.logo,
          type: form.bizType,
        }
      }
      
    })
    
  })
  
  return toFormData
  
}

/** 
 * @description 根据规则表单条件获取连接器配置的 值类型
*/
function getConnectorOptionsFieldOptionValueTypeByRuleForm(ruleFormItem: ConnectorRuleFormItemType): ConnectorOptionsFieldOptionValueTypeEnum | null {
  
  const ruleFormCondition = ruleFormItem?.condition
  
  // 置空
  if (isConnectorRuleFormConditionEmpty(ruleFormCondition)) {
    return null
  }
  
  // 固定值
  if (isConnectorRuleFormConditionFixedValue(ruleFormCondition)) {
    return ConnectorOptionsFieldOptionValueTypeEnum.FixedValue
  }
  
  // 对应字段
  if (isConnectorRuleFormConditionFromField(ruleFormCondition)) {
    return ConnectorOptionsFieldOptionValueTypeEnum.FromField
  }

  // 对应字段
  if (isConnectorRuleFormConditionAppFromField(ruleFormCondition)) {
    return ConnectorOptionsFieldOptionValueTypeEnum.FromAppField
  }
  
  return null
}

/** 
 * @description 根据连接器配置的 值类型 获取 规则表单条件
*/
function getRuleFormConditionByConnectorOptionsFieldOptionValueType(valueType: ConnectorOptionsFieldOptionValueTypeEnum | null): ConnectorConditionTypeEnum {
  
  // 置空
  if (isNull(valueType)) {
    return ConnectorConditionTypeEnum.Empty
  }
  
  // 固定值
  if (valueType == ConnectorOptionsFieldOptionValueTypeEnum.FixedValue) {
    return ConnectorConditionTypeEnum.FixedValue
  }
  
  // 对应字段
  if (valueType == ConnectorOptionsFieldOptionValueTypeEnum.FromField) {
    return ConnectorConditionTypeEnum.FromField
  }

  // 其他表单
  if (valueType == ConnectorOptionsFieldOptionValueTypeEnum.FromAppField) {
    return ConnectorConditionTypeEnum.AppFormField
  }
    
  
  return ConnectorConditionTypeEnum.Empty
}


/** 
 * @description 根据规则表单获取连接器配置的值
*/
function getConnectorOptionsFieldOptionValueByRuleForm(ruleFormItem: ConnectorRuleFormItemType, isSelect?: boolean): string[] {
  
  const ruleFormCondition = ruleFormItem?.condition
  const field = ruleFormItem?.field || {} as ConnectorField
  const formType = field?.formType || ''
  const fieldName = field?.fieldName || ''
  // 后端新增了不为空 传['']
  if (ruleFormItem?.operator === 'NOT_EMPTY') {
    return ['']
  }
  // 置空
  if (isConnectorRuleFormConditionEmpty(ruleFormCondition)) {
    return []
  }
  
  // 固定值
  if (isConnectorRuleFormConditionFixedValue(ruleFormCondition)) {
    
    //  如果取值方式为固定值，操作符为 相等 和 包含 
    //  因为后端新建的时候操作符固定是相等 所以就没有返回操作符 前端这里判断是新建的时候手动赋值相等
    if(!isSelect) ruleFormItem.operator = ConnectorFieldOperateEnum.Equal
    // @ts-ignore
    if (Object.values(ConnectorFieldOperateEnum).indexOf(ruleFormItem.operator) > -1) {
      
      // 如果固定值是对象比如人员 前端传给后端json字符串
      if (isObject(ruleFormItem.value)) {
        // 空对象
        if(isEmpty(ruleFormItem.value)) return ['']
        const value = convertFormValueToServerValue(ruleFormItem.value, field)
        return [JSON.stringify(value)]
      }
      
      // 如果操作符是BETWEEN 则传：["value1", "value2"]
      if (ruleFormItem.operator == ConnectorFieldOperateEnum.Between || ruleFormItem.operator == ConnectorFieldOperateEnum.NOT_BETWEEN) {
        if (isArray(ruleFormItem.value) && ruleFormItem.value.length) {
          return [ruleFormItem.value[0], ruleFormItem.value[1]]
        }
      }

      // 如果值是数组 比如客户
      if (isArray(ruleFormItem.value)) {
        // 空数组
        if(!ruleFormItem.value.length) return ['']
        const value = ruleFormItem.value.map((item: any) => {
          if(isObject(item)) {
            // 触发器知识库选择角色是个对象数组 但是后端需要的是字符串数组['1','2']
            const fieldName = field?.fieldName || field?.enName || ''
            if(fieldName === 'role') {
              item = item.value
            } else {
              item = convertFormValueToServerValue(item, field)
            }
          }
          return item
        })
        
        return [JSON.stringify(value)]
      }
      
      if (isString(ruleFormItem.value)) {
        
        const value = convertFormValueToServerValue(ruleFormItem.value, field)
        
        if (isString(value)) {
          return [value]
        }
        
        return [stringify(value)]
        
      }
      
      return [ruleFormItem.value] as string[]
      
    }


    // 项目管理消息特殊处理
    if (field.fieldName === 'projectStatusId' || field.fieldName === 'taskStatus') {
        // 如果值是数组 项目状态和任务状态 不包含都是数组
        if (isArray(ruleFormItem.value)) {
          // 空数组
          if(!ruleFormItem.value.length) return ['']
          const value = ruleFormItem.value.map((item: any) => {
            if(isObject(item)) {
              // 触发器知识库选择角色是个对象数组 但是后端需要的是字符串数组['1','2']
              item = convertFormValueToServerValue(item, field)
            }
            return item
          })
          return [JSON.stringify(value)]
        }
    }
    
    // 如果取值方式为固定值，操作符为 = ，则传：["固定值"]
    return [ruleFormItem.value]
  }
  
  // 对应字段
  if (isConnectorRuleFormConditionFromField(ruleFormCondition)) {
    return [ruleFormItem.rightFieldValue]
  }

  //  外部表单
  if (isConnectorRuleFormConditionAppFromField(ruleFormCondition)) {
    return [convertTriggerAppFormFieldValue(ruleFormItem.value, isSelect)]
  }
  
  return []
}

function convertTriggerAppFormFieldValue(value = '', isSelect = false, reverse = false, fieldList: ConnectorToField[] = []) {
  let appFromFieldValue = ''
  try {
    if(!isString(value)) return ''
    const parsedValue = JSON.parse(value)
    const convertRequestV = parsedValue?.request.map((item: any) => {
      return reverse ? ConnectorOptionsFieldOptionToConnectorRuleForm(item, fieldList, isSelect) : item.componentName ? connectorRuleFormToConnectorOptionsFieldOption(item || [], isSelect) : item
    })
    parsedValue.request = convertRequestV
    appFromFieldValue = JSON.stringify(parsedValue)
  } catch (error) {
    console.log('JSON.parse--isConnectorRuleFormConditionAppFromField failed', error)
  }
  return appFromFieldValue
}

/** 
 * @description 根据连接器配置的值获取规则表单的值
*/
function getRuleFormValueByConnectorOptionsFieldOptionValue(fieldOption: ConnectorOptionsFieldOption, toFieldList: ConnectorToField[], isSelect: boolean): Partial<ConnectorRuleFormItemType> {
  const valueType = fieldOption?.valueType
  const operate = fieldOption?.operate
  const value: any = fieldOption?.value || []
  
  const formType = getFieldTypeByFieldName(fieldOption?.toEnName as string, toFieldList)
  const fieldName = fieldOption?.toEnName || ''

  // 置空
  if (isNull(valueType)) {
    return {
      value: '',
      rightFieldValue: ''
    }
  }
  // 固定值
  if ((valueType == ConnectorOptionsFieldOptionValueTypeEnum.FixedValue)) {
    
    let firstValue: any = ''
    
    // TODO: 
    const field = {
      formType,
      fieldName
    } as unknown as Field
    
    const options = {
      field,
      operator: operate,
      isInsert: !isSelect,
      isConnector: true
    }
    
    // TODO: 修改
    const { component, bind } = genComponent(options)
    
    if(operate === ConnectorFieldOperateEnum.Between) {
      // 日期格式数据
      // 新建的时候只有一个值 查询的时候是[start, end]
      firstValue = isSelect ? value : value[0]
    } else {
      
      // 这里还需要处理对象格式的值
      try {
        firstValue = JSON.parse(value)
        // 如果是['0']这样的JSON.parse以后就变成0
        if(isNumber(firstValue)) {
          firstValue = String(firstValue)
        }
      } catch (error) {
        console.warn('解析失败')
        firstValue = value?.[0]
      }
      
      // console.log('value', value, firstValue)
      
    }
    
    return {
      formType,
      value: firstValue,
      rightFieldValue: '',
      componentName: component,
      bind
    }
    
  }
  
  // 对应字段
  if (valueType == ConnectorOptionsFieldOptionValueTypeEnum.FromField) {
    
    let rightFieldValue = value[0] || ''

    // json类型
    if (formType == ConnectorFieldTypeEnum.JsonArray || formType == ConnectorFieldTypeEnum.JsonObject) {
      
      try {
        let fieldValue = JSON.parse(rightFieldValue)

        if (isObject(fieldValue)) rightFieldValue = fieldValue?.fromEnName || ''

      } catch(err) {
        rightFieldValue = ''
        console.error('getRuleFormValueByConnectorOptionsFieldOptionValue:', err)
      }
      
    }
    
    return {
      value: '',
      rightFieldValue
    }
    
  }


  // 其他表单数据直接用json字符串 所以跟固定值放一起了
  if(valueType === ConnectorOptionsFieldOptionValueTypeEnum.FromAppField) {
    return {
      value: value?.[0] || '',
      rightFieldValue: ''
    }
  }
  
  return {
    value: '',
    rightFieldValue: ''
  }
}

/** 
 * @description 根据后端返回的enName从toFieldList里获取对应的字段的fieldType
*/
function getFieldTypeByFieldName(enName: string, toFieldList: ConnectorToField[]): string { 
  
  if(!enName) return ''
  
  return toFieldList.find((item: ConnectorToField) => item.enName === enName)?.fieldType || ''
}

/** 
 * @description 根据规则表单条件获取连接器配置
*/
function connectorRuleFormToConnectorOptionsFieldOption(ruleFormItem: ConnectorRuleFormItemType, isSelect?: boolean): ConnectorOptionsFieldOption {
  
  // 字段名
  const toEnName = ruleFormItem.leftFieldValue
  // 取值方式  1：固定值，2：对应字段 3 外部表单
  const valueType = getConnectorOptionsFieldOptionValueTypeByRuleForm(ruleFormItem)
  // 操作符
  const operate = ruleFormItem.operator
  // 值
  const value: string[] = getConnectorOptionsFieldOptionValueByRuleForm(ruleFormItem, isSelect)
  
  return {
    toEnName,
    valueType,
    operate,
    value
  }
}

/** 
 * @description 根据规则表单条件获取连接器配置
*/
function ConnectorOptionsFieldOptionToConnectorRuleForm(fieldOption: ConnectorOptionsFieldOption, toFieldList: ConnectorToField[], isSelect: boolean): ConnectorRuleFormItemType {
  
  const operator = fieldOption.operate
  const valueType = fieldOption.valueType
  const leftFieldValue = fieldOption.toEnName
  const condition = getRuleFormConditionByConnectorOptionsFieldOptionValueType(valueType)
  // 值
  const formValue: Partial<ConnectorRuleFormItemType> = getRuleFormValueByConnectorOptionsFieldOptionValue(fieldOption, toFieldList, isSelect)
  
  const rightFieldValue = formValue?.rightFieldValue || ''
  const componentName = formValue?.componentName || ''
  const bind = formValue?.bind || {}
  const field = toFieldList.find(item => {return item.enName === leftFieldValue}) || null
  const required = field?.required || false
  const parentEnName = field?.parentEnName || ''
  let value = formValue.value
  
  if (formValue?.value) {
    
    value = formValue.value
    
  } else {
    
    const formField = connectorToField2FormField(field as ConnectorToField)
    const result = initialize([formField])
    
    value = result[formField.fieldName]
    
  }
  
  const ruleFormItem: ConnectorRuleFormItemType = {
    id: uuid(),
    operator,
    condition,
    leftFieldValue,
    value,
    operators: [],
    rightFieldValue,
    componentName,
    bind,
    // @ts-ignore
    field,
    required,
    parentEnName,
  }
  
  return ruleFormItem
}

/** 
 * @description 转换 组件数据 到 服务端接收的数据。
*/
function connectComponentValueToServerValue(
  nameValue: Record<CreateConnectorDialogFieldNameEnum, string | string[]>,
  detailValue: ConnectorDialogDetailData,
  fromBizTypeIdString: string,
  fromBizTypeValue: string | ConnectorBizTypeEnum,
  toBizTypeValue: string | ConnectorBizTypeEnum,
): ConnectorSaveOptionsParams {
  
  // 用于区分编辑还是新增
  const additionalId = (detailValue.additionalId || null) as number | null
  // 连接器名称
  const connectorName = nameValue.name as string
  
  // 连接器说明
  const connectorRemarks = nameValue.description as string
  
  // 连接器名称国际化语言
  const titleLanguage = nameValue.titleLanguage as any
  
  // 连接器说明国际化语言
  const descLanguage = nameValue.descLanguage as any
  
  // from 表单业务类型
  const fromBizType = fromBizTypeValue as ConnectorBizTypeEnum
  
  // from 表单id
  const fromBizTypeId = fromBizTypeIdString
  
  // to 表单id
  const toBizTypeId = detailValue.paasApp.id
  
  // to 表单业务类型
  const toBizType = toBizTypeValue as ConnectorBizTypeEnum
  
  // to 表单logo
  const toBizTypeLogo = detailValue.paasApp.icon
  
  // to 表单名称
  const toBizTypeName = detailValue.paasApp.name
  
  // 执行动作列表
  const actionList = detailValue.actionsValue
  
  // 附加组件类型 单次 或 多次 
  const addTime = detailValue.inputType
  
  // 表格显示的表单字段列表
  const showFieldNameList: string[] = detailValue.showFieldNameList
  // 查询范围设置条件 1 忽略此条件 2 不展示数据
  const conditionalLimit = detailValue.conditionalLimit
  // 当前节点状态
  const stateCanEdit = detailValue.stateCanEdit
  
  const params: ConnectorSaveOptionsParams = {
    additionalId,
    connectorName,
    connectorRemarks,
    titleLanguage,
    descLanguage,
    fromBizType,
    fromBizTypeId,
    toBizTypeId,
    toBizType,
    toBizTypeLogo,
    toBizTypeName,
    actionList,
    addTime,
    showFieldNameList,
    conditionalLimit,
    stateCanEdit,
  }
  
  
  // 根据执行动作确定上传参数
  if (checkIsExistAction(params.actionList, ConnectorOptionsActionEnum.Select)) {
    
    // 查询配置规则
    const selectFieldOptionsList: ConnectorOptionsFieldOption[] = (
      detailValue.selectRuleForms.map(item=> connectorRuleFormToConnectorOptionsFieldOption(item, true))
    )
    
    params.selectFieldOptionsList = selectFieldOptionsList
    
  }
  
  if (checkIsExistAction(params.actionList, ConnectorOptionsActionEnum.Insert)) {
    
    // 新建插入配置规则
    const insertFieldOptionsList: ConnectorOptionsFieldOption[] = (
      detailValue.insertRuleForms.map(item=> connectorRuleFormToConnectorOptionsFieldOption(item, false))
    )
    
    params.insertFieldOptionsList = insertFieldOptionsList
    
  }

  params.actionList = params.actionList.map((actionItem: ConnectorSaveOptionsParamsActionListItem) => {
    const { apiId, name, action} = actionItem
    return {
      apiId,
      name,
      action
    }
  }) as unknown as ConnectorSaveOptionsParamsActionListItem []
  
  return params
}

/** 
 * @description 转换 服务端接收的数据到组件数据 给工单到paas用的。
*/
function connectServerValueToComponentValueForEditWithTaskToPaas(
  connectorEditOptions: ConnectorEditOptions
): ConnectorDialogDetailData {
  
  const paasApp: ConnectorDialogDetailPaasData = {
    id : connectorEditOptions?.toBizTypeId || '',
    name : connectorEditOptions?.toBizTypeName || '',
    icon : connectorEditOptions?.toBizTypeLogo || '',
    type : connectorEditOptions?.toBizType || '',
  }
  
  const actionList = connectorEditOptions?.actionList || []
  const actionsValue = actionList.filter(item=> item.selected)
  
  const insertFields = actionsValue.find(item=> item.action === ConnectorOptionsActionEnum.Insert)?.toRequestFieldList || []
  const insertRuleForms = isEmpty(connectorEditOptions?.insertFieldOptionsList) 
    ? [createConnectorRuleFormItem()] 
    : connectorEditOptions.insertFieldOptionsList.map(item=>ConnectorOptionsFieldOptionToConnectorRuleForm(item, insertFields, false))
  
  const selectFields = actionsValue.find(item=> item.action === ConnectorOptionsActionEnum.Select)?.toRequestFieldList || []
  const selectRuleForms = isEmpty(connectorEditOptions?.selectFieldOptionsList)
    ? [createConnectorRuleFormItem()]
    : connectorEditOptions.selectFieldOptionsList.map(item=>ConnectorOptionsFieldOptionToConnectorRuleForm(item, selectFields, true))
  
  const detailValue: ConnectorDialogDetailData = {
    additionalId: connectorEditOptions.additionalId as unknown as string,
    bizTypeId: connectorEditOptions.fromBizTypeId,
    paasApp,
    actionsValue,
    actionList,
    fromFieldList: connectorEditOptions.fromFieldList || [],
    toFieldList: connectorEditOptions.toFieldList || [],
    insertRuleForms,
    selectRuleForms,
    showFieldNameList: connectorEditOptions.showFieldNameList || [],
    inputType: connectorEditOptions.addTime,
    appType: connectorEditOptions.appType,
    conditionalLimit: connectorEditOptions.conditionalLimit || '1'
  }
  
  return detailValue
  
}

function getPaasUrlByBizId(bizId: string, originUrl: string) {
  return originUrl.replace('$bizId$', bizId)
}

function replacePaasUrlByProcessorInstanceId(processorInstanceId: string, originUrl: string) {
  return originUrl.replace('$processorInstanceId$', processorInstanceId)
}

function replaceTaskUrlByTaskId(taskId: string, originUrl: string) {
  return originUrl.replace('$taskId$', taskId)
}

// 项目详情id
function replaceTaskUrlByProjectId(id: string, originUrl: string) {
  return originUrl.replace('$id$', id)
}

function replaceTaskUrlByToBizTypeId(originUrl: string, toBizTypeId: string) {
  return originUrl.replace('$taskTypeId$', toBizTypeId)
}

function getTabIdByConnectorJumpUrl(connectorJumpUrl: string) {
  return connectorJumpUrl.replaceAll('/', '').replaceAll('-', '').replaceAll('#', '').replaceAll('?', '').replaceAll('&', '').replaceAll('=', '')
}

/**
 * @description: 解析固定值value
 * @param {ConnectorRuleFormItemType} item
 * @return {*}
 */
function transformFixedValue(item: ConnectorRuleFormItemType){
  //todo
}

function addConnectorModuleUniqueId(connectorModuleList: ConnectorGetModuleListResult) {
  
  connectorModuleList.forEach(connectorModule => {
    connectorModule.children.forEach(connectorModuleItem => {
      connectorModuleItem.uniqueId = `${connectorModuleItem.bizTypeId}-${connectorModuleItem.name}`
    })
  })
  
  return connectorModuleList
}

function addConnectorModuleUniqueIdEveryLevel(connectorModuleList: ConnectorGetAppModuleListItemResult, needName = true) {
  
  connectorModuleList.forEach(connectorModuleItem => {
    // 加connectorModuleItem.id是为了兼容触发器
    connectorModuleItem.uniqueId = needName ? `${connectorModuleItem.bizTypeId || connectorModuleItem.id}-${connectorModuleItem.name}` : (connectorModuleItem.bizTypeId || connectorModuleItem.id) as unknown as string
    if(connectorModuleItem.children && connectorModuleItem.children.length > 0) {
      addConnectorModuleUniqueIdEveryLevel(connectorModuleItem.children, needName)
    } 
  })
  
  return connectorModuleList
}

function checkIsExistAction(actionList: ConnectorSaveOptionsParamsActionListItem[], action: ConnectorOptionsActionEnum ) {
  return actionList.some(item => item.action === action)
}



// 处理jsonArray/jsonObject类型 toFieldList 字段
function convertJsonFieldTypeToFieldList(toFieldList: ConnectorToField [] = []) {
  let result: ConnectorToField[] = []

  toFieldList.forEach(field => {
    const { enName, fieldType, children = [] } = field

    field.parentEnName = ''
    delete field.children

    result.push(field)

    if (fieldType === ConnectorFieldTypeEnum.JsonArray || fieldType === ConnectorFieldTypeEnum.JsonObject) {

      children.forEach(child => {
        child.parentEnName = enName
        
        // 拼接 确保唯一性
        child.enName = `${enName}-${child.enName}`
      })

      result = result.concat(...children)
    }
  })
  return result
}

export {
  connectorModuleToFormCascaderDataSource,
  getToFormFromModuleListById,
  connectComponentValueToServerValue,
  connectServerValueToComponentValueForEditWithTaskToPaas,
  getPaasUrlByBizId,
  replacePaasUrlByProcessorInstanceId,
  replaceTaskUrlByToBizTypeId,
  replaceTaskUrlByTaskId,
  replaceTaskUrlByProjectId,
  getConnectorOptionsFieldOptionValueByRuleForm,
  connectorRuleFormToConnectorOptionsFieldOption,
  getTabIdByConnectorJumpUrl,
  addConnectorModuleUniqueId,
  ConnectorOptionsFieldOptionToConnectorRuleForm,
  connectorAppModuleToFormCascaderDataSource,
  addConnectorModuleUniqueIdEveryLevel,
  checkIsExistAction,
  convertTriggerAppFormFieldValue,
  convertJsonFieldTypeToFieldList
}
